import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/pages/Home'
import About from '@/pages/About'
import Message from '@/pages/Message'
import News from '@/pages/News'
import Detail from '@/pages/Detail'

Vue.use(VueRouter)

// 将VueRouter原型上原始的push保存一份
const originPush = VueRouter.prototype.push
// 修改VueRouter原型上的push
VueRouter.prototype.push = function(location,onSuccess,onError){
  if(onSuccess || onError){
    return originPush.call(this,location,onSuccess,onError)
  }else{
    return originPush.call(this,location).catch(()=>{})
  }
}

// 将VueRouter原型上原始的push保存一份
const originReplace = VueRouter.prototype.replace
// 修改VueRouter原型上的push
VueRouter.prototype.replace = function(location,onSuccess,onError){
  if(onSuccess || onError){
    return originReplace.call(this,location,onSuccess,onError)
  }else{
    return originReplace.call(this,location).catch(()=>{})
  }
}


export default new VueRouter({
  mode:'history',
  routes:[
    {
      path:'/home',
      // name:'zhuye',
      component:Home,
      children:[
        {
          path:'message',
          name:'xiaoxi',
          component:Message,
          children:[
            {
              path:'detail',
              name:'xiangqing',
              component:Detail,
              // props的第二种写法，值为布尔值，可以把该路由规则收到的params参数一个一个的传给路由组件
              // props:true

              // props的第三种写法，值为函数
              props($route){
                return {...$route.query,a:100}
              }
            }
          ]
        },
        {
          path:'news',
          name:'xinwen',
          component:News
        },
        {
          path:'',
          redirect:'/home/news'
        }
      ]
    },
    {
      path:'/about',
      name:'guanyu',
      component:About,
    },
    {
      path:'',
      redirect:'/about'
    }
  ]
})


